﻿package at.geoathome.ui.buttons
{
	import at.geoathome.ui.UIUtils;
	import at.geoathome.ui.buttons.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.utils.getDefinitionByName;
	
	/**
	* @author Georg Kamptner
	*/
	public class Button extends MovieClip implements IButton
	{
		protected var _bg:MovieClip;
		protected var _enabled:Boolean;
		protected var _activated:Boolean;
		protected var _over_state:Boolean;
		protected var _style:String;
		
		public static var STATE_GLUE:String 				= "_" ;
		public static const STATE_OVER :String				= "over";
		public static const STATE_OUT :String				= "out";
		public static const STATE_ENABLED :String			= "enabled";
		public static const STATE_DISABLED :String			= "disabled";
		public static const STATE_ACTIVATED	:String			= "activated";
		public static const STATE_DEACTIVATED	:String		= "deactivated";
		public static const INVISIBLE_BACKGROUND :String	= "1447_ButtonInvisibleBg";
		
		public function Button(button_bg_classname:String = Button.INVISIBLE_BACKGROUND) 
		{
			// create bg
			if (button_bg_classname != Button.INVISIBLE_BACKGROUND) {
				_bg = new (getDefinitionByName(button_bg_classname))();
			} else {
				_bg = new MovieClip();
				_bg.graphics.beginFill(0xff0000, 0.0);
				_bg.graphics.drawRect(0, 0, 1, 1);
				_bg.graphics.endFill();
			}
			_bg.x = 0;
			_bg.y = 0;
			_bg.gotoAndStop(1);
			this.addChild(_bg);
			
			// make mc to button
			this.mouseEnabled = true;
			this.useHandCursor = true;
			this.buttonMode = true;
			// event listeners
			this.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			this.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
			
			// set initial state
			_style = "";
			_over_state = false;
			_enabled = true;
			_activated = false;
			if(_bg.hitTestPoint(_bg.mouseX, _bg.mouseY, true))
				_over_state = true;
			goToStateLabelFrame();
		}
		
		public override function get enabled():Boolean {
			return _enabled;
		}
		
		public override function set enabled(enabled:Boolean):void
		{
			if (enabled)
				enable();
			else
				disable();
		}
		
		public function get activated():Boolean {
			return _activated;
		}
		
		public function set activated(activated:Boolean):void 
		{
			if (activated)
				activate();
			else
				deactivate();
		}
		
		public function get style():String {
			return _style;
		}
		
		public function set style(style:String):void
		{
			_style = style;
			goToStateLabelFrame();
		}
		
		protected function goToStateLabelFrame():void
		{
			var style_prefix:String = (_style.length == 0) ? "" : _style + Button.STATE_GLUE;
			var label_name_parts:Array = new Array(
													(_over_state) ? Button.STATE_OVER : Button.STATE_OUT ,
													(_enabled) ? Button.STATE_ENABLED : Button.STATE_DISABLED ,
													(_activated) ? Button.STATE_ACTIVATED : Button.STATE_DEACTIVATED
												  );
											
			
			// try the full label name
			var label_name:String = style_prefix + label_name_parts[0] + Button.STATE_GLUE + label_name_parts[1] + Button.STATE_GLUE + label_name_parts[2];
			if (UIUtils.indexOfFrameLabel(_bg, label_name)) {
				_bg.gotoAndStop(label_name);
			} else {
				
				// try a shorter label name
				label_name = style_prefix + label_name_parts[0] + Button.STATE_GLUE + label_name_parts[1];
				if (UIUtils.indexOfFrameLabel(_bg, label_name)) {
					_bg.gotoAndStop(label_name);
				} else {
					
					// try an even shorter label name
					label_name = style_prefix + label_name_parts[0];
					if (UIUtils.indexOfFrameLabel(_bg, label_name)) {
						_bg.gotoAndStop(label_name);
					} else {
						// no label found :(
					}
				}
			}
		}
		
		protected function onMouseOver(e:MouseEvent):void
		{	
			if (_over_state) return;
			_over_state = true;
			
			goToStateLabelFrame();
		}
		
		protected function onMouseOut(e:MouseEvent):void
		{
			if (!_over_state) return;
			_over_state = false;
			
			goToStateLabelFrame();
		}
		
		public function enable(e:Event = null):void
		{
			if (_enabled) return;
			_enabled = true;
			
			// enable mouse interactivity
			this.mouseEnabled = true;
			this.buttonMode = true;
			this.mouseChildren = true;
			
			goToStateLabelFrame();
		}
		
		public function disable(e:Event = null):void
		{
			if (!_enabled) return;
			_enabled = false;
			
			// disable mouse interactivity
			this.mouseEnabled = false;
			this.buttonMode = false;
			this.mouseChildren = false;
			
			goToStateLabelFrame();
		}
		
		public function activate():void
		{
			if (_activated) return;
			_activated = true;
			
			goToStateLabelFrame();
		}
		
		public function deactivate():void
		{
			if (!_activated) return;
			_activated = false;
			
			goToStateLabelFrame();
		}
		
		public override function set width(width:Number):void
		{
			_bg.width = width;
		}
		
		public override function set height(height:Number):void
		{
			_bg.height = height;
		}
	}
	
}